
$(document).ready(function(){
	$('#login-but').on('click', function(){
		event.preventDefault();
		$("#login-bg").fadeIn(300);
		$('#login-bg').css('visibility','visible');
		$('#login-form').css('visibility','visible');
	})

	$('#reg-but').on('click', function(){
		event.preventDefault();
		$("#login-bg").fadeIn(300);
		$('#login-bg').css('visibility','visible');
		$('#registration-form').css('visibility','visible');
	})
	
	$('#login-bg').on('click', function(){
		$('#login-bg').css('visibility','hidden');
		$('#login-form').css('visibility','hidden');
		$('#registration-form').css('visibility','hidden');
	})


	function ajaxrequest(url, typeReq, data, onSuccess){
		$.ajax({
			url: url,
			type: typeReq,
			data: data,
			async: false,
			success: onSuccess
		});
	}

	function fadeInLogin(resp, pic){
		$('#login-form').css('visibility', 'hidden');
				$('#message-box').css('visibility', 'visible');
				$("#login-bg").fadeIn(300);
				$("#message-box").fadeIn(300);
				$('#message-box').html("<div id='message'></div>");
				$('#message').html("<h2>"+resp+"</h2>")
				.hide()  
    			.fadeIn(1500, function() {  
      				$('#message').append("<img id='checkmark' src='images"+pic+"'/>");  
    			});
	}

	function ajaxGetRequest(url, onSuccess){
		$.ajax({
			url: url,
			type: 'GET',
			success: onSuccess
		});
	}


	function adminAjax(data, onSuccess){
		$.ajax({
				url: 'admin.php',
				type: 'POST',
				data: data,
				success: onSuccess
			});
	}

	function userAjax(data, onSuccess){
		$.ajax({
				url: 'userControlPanel.php',
				type: 'POST',
				data: data,
				success: onSuccess
			});
	}

	function adminRequests(action){
		$('#admin-forms-container').empty();
			var data = {typeAction: action};
			adminAjax(data, function(data){
				$('#admin-forms-container').append(data);
			});
	}

	function userRequests(action){
		$('#userset-forms-container').empty();
			var data = {typeAction: action};
			userAjax(data, function(data){
				$('#userset-forms-container').append(data);
				console.log(data);
			});
	}



	function callHallsCombo(cinemaname, hallcombo){
		$(document).on('change', cinemaname, function(){
			var cinema_name = $(cinemaname).val();
			var cindata = {cinemaname: cinema_name, typeAction: "41"};
			ajaxrequest('admin.php', 'POST', cindata, function(data){
				$(hallcombo).empty();
				$(hallcombo).append(data);
			});
		})
	}


	function covers(){
		ajaxGetRequest('changeCovers.php', function(data){
			var resp = JSON.parse(data);
			$('#cover1').children('img').attr("src", "movie-covers"+resp[0]);
			$('#cover2').children('img').attr("src", "movie-covers"+resp[1]);
			$('#cover3').children('img').attr("src", "movie-covers"+resp[2]);
			$('#cover4').children('img').attr("src", "movie-covers"+resp[3]);
			$('#cover5').children('img').attr("src", "movie-covers"+resp[4]);
		});
	}
	covers();
	setInterval(function(){
		covers();
	}, 10000);


	function homePage(){
		$('#maincontent').empty();
		ajaxGetRequest('home.php', function(data){
			$('#maincontent').append(data);
		});
	}

	function checkSession(){
		ajaxGetRequest('checkSession.php', function(data){
		});
	}

	function messageBox(data, textsize){
		$('#message-box').css('visibility', 'visible');
		$("#message-box").fadeIn(300);
		$('#message-box').html("<div id='message'></div>");
		$('#message').html("<h"+textsize+">"+data+"</h"+textsize+">")
		.hide()  
    	.fadeIn(1500, function() {  
      	$('#message').append("<img id='checkmark' src='images/check.png' />"); 
    	});
    	$("#message-box").delay(3200).fadeOut(300); 
	}
	
	checkSession();
	homePage();


	//Login, Logout Registration
	var rform = $('#reg-form');
	rform.submit(function(event){
		ajaxrequest(rform.attr('action'), rform.attr('method'), rform.serialize(), function(data){
				var resp = JSON.parse(data);
				console.log(resp.success);
				if (resp.success == 1) {
					var name = $('#username').val();
					var password = $('#repass').val();
					var send_data = {user: name, pass: password};
					console.log(send_data);
					ajaxrequest('login.php', 'post', send_data, function(data1){
						console.log(data1);
						var resp1 = JSON.parse(data1);
						if (resp1.response == "0") {
							if (resp1.utype == "0") {
								$('.out-set:last').css('visibility', 'visible');
							}
							else {
								$('.out-set:first').css('visibility', 'visible');
							};
			    			$('#who-am-i').text("Welcome " + resp1.uusername);
							$('#hidlog').css('visibility', 'hidden');
							$('#who-am-i').css('visibility', 'visible');
							homePage();
						}
						else {
							fadeInLogin(resp.msg);
			    			$("#login-bg").delay(3200).fadeOut(300);
			    			$("#message-box").delay(3200).fadeOut(300);
						}		
					});
					$('#registration-form').css('visibility', 'hidden');
					$("#login-bg").fadeIn(300);
					messageBox(resp.mess, 2);  
    				$("#login-bg").delay(3200).fadeOut(300);
					$('#pass1').val("");
					$('#repass').val("");
				};
				
			});
		event.preventDefault();
	});

	
	var lform = $('#log-form');
	lform.submit(function(){
		ajaxrequest(lform.attr('action'), lform.attr('method'), lform.serialize(), function(data){
			var resp = JSON.parse(data);
			if (resp.response == "0") {
				if (resp.utype == "0") {
					$('.out-set:last').css('visibility', 'visible');
				}
				else {
					$('.out-set:first').css('visibility', 'visible');
				};
				fadeInLogin(resp.msg, resp.pic);
    			$("#login-bg").delay(3200).fadeOut(300);
    			$("#message-box").delay(3200).fadeOut(300);
    			$('#who-am-i').text("Welcome " + resp.uusername);
				$('#hidlog').css('visibility', 'hidden');
				$('#who-am-i').css('visibility', 'visible');
				homePage();
				$('#user').val("");
				$('#pass').val("");
			}
			else {
				fadeInLogin(resp.msg);
    			$("#login-bg").delay(3200).fadeOut(300);
    			$("#message-box").delay(3200).fadeOut(300);
			}		
		});
		event.preventDefault(); 
	});
	


	$('.logout-but').on('click', function(){
		event.preventDefault();
		var url = 'logout.php';
		var type = 'POST';
		var data = { logout: 1};
		ajaxrequest(url, type, data, function(data){
			homePage();
			});
		$("#login-bg").fadeIn(300);
		$("#message-box").fadeIn(300);
		$("#who-am-i").css('visibility', 'hidden');
		$('#message-box').html("<div id='message'></div>");
				$('#message').html("<h3>You are logging out. Bye!</h3>")
				.hide()  
    			.fadeIn(1500, function() {  
      				$('#message').append("<img id='checkmark' src='images/check.png' />");  
    			});
    			$("#login-bg").delay(3200).fadeOut(300);
    			$("#message-box").delay(3200).fadeOut(300);
		$('#hidlog').css('visibility', 'visible');
		$('.out-set').css('visibility', 'hidden');
		$('#add-rv-cinema').css('visibility', 'hidden');
	})

	//Search
	$('#search-button').on('click', function(){
		event.preventDefault();
		var search_word = $('#search-text').val();
		$('#maincontent').empty();
		if (search_word!="") {
			var send_data = {word: search_word};
			ajaxrequest('searchMovie.php', 'POST', send_data, function(data){
				$('#maincontent').append(data);
				$('#search-text').val("");
			});
		};
	})

	//Admin panel

	$('#admin-but').on('click', function(){
		event.preventDefault();
		$('#maincontent').empty();
		$('#maincontent').append('<div id="admin-action-buttons"><ul><li id="add-cin-but"><a href="#">Add Cinema</a></li><li id="rv-cin-but"><a href="#">Remove cinema</a></li><li id="add-hall-but"><a href="#">Add Hall</a></li><li id="add-movies-but"><a href="#">Add Movie</a></li><li id="ch-movies-but"><a href="#">Change Movie</a></li><li id="add-proj-but"><a href="#">Add Projection</a></li><li id="account-but"><a href="#">Activate/Deactivate account</a></li></ul></div><div id="admin-forms-container"></div>');

	})

	$(document).on('click', '#add-cin-but', function(){
		adminRequests("1");
	})
	$(document).on('click', '#rv-cin-but', function(){
		adminRequests("2");
	})
	$(document).on('click', '#add-hall-but', function(){
		adminRequests("3");
	})
	$(document).on('click', '#rv-hall-but', function(){
		adminRequests("4");
	})
	$(document).on('click', '#add-movies-but', function(){
		adminRequests("5");
	})
	$(document).on('click', '#ch-movies-but', function(){
		adminRequests("6");
	})
	$(document).on('click', '#add-proj-but', function(){
		adminRequests("7");
	})
	$(document).on('click', '#account-but', function(){
		adminRequests("8");
	})

	//Admin - Cinema buttons

	$(document).on('submit', '#add-cinema',function(event){
		var add_cinema_form = $('#add-cinema');
		ajaxrequest(add_cinema_form.attr('action'), add_cinema_form.attr('method'), add_cinema_form.serialize(), function(data){
			messageBox(data, 4);
			adminRequests("1");
		}); 
		event.preventDefault();
	})
	
	$(document).on('submit', '#rv-cinema',function(event){
		var rv_cinema_form = $('#rv-cinema'); 
		ajaxrequest(rv_cinema_form.attr('action'), rv_cinema_form.attr('method'), rv_cinema_form.serialize(), function(data){
			messageBox(data, 4);
			adminRequests("2");
		});
		event.preventDefault();
	})

	//Admin - Halls buttons
	$(document).on('submit', '#add-hall',function(event){
		var add_hall_form = $('#add-hall');
		ajaxrequest(add_hall_form.attr('action'), add_hall_form.attr('method'), add_hall_form.serialize(), function(data){
			messageBox(data, 4);
			adminRequests("3");
		}); 
		event.preventDefault();
	})

	// $(document).on('submit', '#rv-hall',function(event){
	// 	var rv_hall_form = $('#rv-hall');
	// 	ajaxrequest(rv_hall_form.attr('action'), rv_hall_form.attr('method'), rv_hall_form.serialize(), function(data){
	// 		messageBox(data, 4); 
	// 	}); 
	// 	event.preventDefault();
	// })

	
	//Admin - Movies buttons
	$(document).on('submit', '#add-movies',function(){
		var add_movies_form = $('#add-movies');
		ajaxrequest(add_movies_form.attr('action'), add_movies_form.attr('method'), add_movies_form.serialize(), function(data){
			messageBox(data, 4);
			adminRequests("5");
		});
		event.preventDefault();
	})

	$(document).on('submit', '#ch-movies',function(){
		event.preventDefault();
		var ch_movies_form = $('#ch-movies');
		ajaxrequest(ch_movies_form.attr('action'), ch_movies_form.attr('method'), ch_movies_form.serialize(), function(data){
			messageBox(data, 4);
			adminRequests("6");
		});
		event.preventDefault();
	})

	callHallsCombo('#rem_hall_sel_cin','#rem_hall');
	callHallsCombo('#add_proj_sel_cin', '#add_proj_hall');

	//Admin - Projection buttons
	$(document).on('submit', '#add-proj',function(){
		var add_proj_form = $('#add-proj');
		ajaxrequest(add_proj_form.attr('action'), add_proj_form.attr('method'), add_proj_form.serialize(), function(data){
			messageBox(data, 4);
			adminRequests("7");
		});
		event.preventDefault();
	})

	//Admin - Activate/Deactivate buttons
	$(document).on('submit', '#account-deact', function(){
		ajaxrequest($('#account-deact').attr('action'), $('#account-deact').attr('method'), $('#account-deact').serialize(), function(data){
			messageBox(data, 4);
			adminRequests("8");
		});
		event.preventDefault();
	})


	//User control panel
	$(document).on('click', '#set-but', function(){
		event.preventDefault();
		$('#maincontent').empty();
		$('#maincontent').append('<div id="account-set-buttons"><ul><li id="change-password-but"><a href="#">Change password</a></li><li id="change-fav-genres-but"><a href="#">Change favourite genres</a></li><li id="deactivate-account-but"><a href="#">Deactivate account</a></li><ul></div><div id="userset-forms-container"></div>');		
	})

	$(document).on('click', '#change-password-but', function(){
		userRequests("1");
	})

	$(document).on('click', '#change-fav-genres-but', function(){
		userRequests("2");
	})

	$(document).on('click', '#deactivate-account-but', function(){
		userRequests("3");
	})

	$(document).on('submit', '#ch-password', function(){
		event.preventDefault();
		ajaxrequest($('#ch-password').attr('action'), $('#ch-password').attr('method'), $('#ch-password').serialize(), function(data){
			messageBox(data, 3);
			userRequests("1");
		});
	})

	$(document).on('submit', '#ch-genres', function(){
		event.preventDefault();
		ajaxrequest($('#ch-genres').attr('action'), $('#ch-genres').attr('method'), $('#ch-genres').serialize(), function(data){
			messageBox(data, 3);
			userRequests("2");
		});
	})

	$(document).on('submit', '#selfdeact-account', function(){
		event.preventDefault();
		ajaxrequest($('#selfdeact-account').attr('action'), $('#selfdeact-account').attr('method'), $('#selfdeact-account').serialize(), function(data){
			messageBox(data, 3);
			userRequests("3");
	    	homePage();
	    	$("#who-am-i").css('visibility', 'hidden');
	    	$('#hidlog').css('visibility', 'visible');
			$('.out-set').css('visibility', 'hidden');
		});
	})


	//Navigation buttons
	$('#home').on('click', function(){
		event.preventDefault();
		homePage();
	})

	$('#cinamas').on('click', function(event){
		event.preventDefault();
		$('#maincontent').empty();
		ajaxGetRequest('cinema.php', function(data){
			$('#maincontent').append(data);
		});
	})

	$('#movie').on('click', function(){
		event.preventDefault();
		$('#maincontent').empty();
		ajaxGetRequest('movies.php', function(data){
			$('#maincontent').append(data);
		});
	})

	$('#future').on('click', function(){
		event.preventDefault();
		$('#maincontent').empty();
		ajaxGetRequest('upcommingMovies.php', function(data){
			$('#maincontent').append(data);
		});
	})


	//Movie List
	$(document).on('click', '.movie-name', function(){
		$('#movie_descr').remove();
		$('#maincontent').append('<div id="movie_descr"></div>');
		event.preventDefault();
		var name = $(this).children('a').text();
		var send_data = { moviename: name};
		ajaxrequest('movieDescription.php', 'POST', send_data, function(data){
			$('#movie_descr').empty();
			$('#movie_descr').append(data);
		});
	})


	//Program actions

	$('#program').on('click', function(){
			event.preventDefault();
			$('#maincontent').empty();
			ajaxGetRequest('programCinemaDate.php', function(data){
				$('#maincontent').append(data);
			});
		})


	$(document).on('click', '.cin_program', function(){
		event.preventDefault();
		var cin_name = $(this).children('a').text();
		var date_index = '1';
		var current_date = '2013-07-02';
		var send = {cinemaname: cin_name, index: date_index, program_date: current_date};
		ajaxrequest('programContent.php', 'POST', send, function(data){
			$('#form_date').remove();
			$("<div id='form_date'><form id='show_program_date' method='post' action='programContent.php'><label for='program_date'>You can choose different date: </label><input type='date' name='program_date' id='diff_chosen_date'/><input type='submit' name='showProgram' value='Show me'/></form></div>").insertAfter('#cin_program_select');
			$('#program_content').remove();
			$('#maincontent').append('<div id="program_content"></div>');
			$('#program_content').append(data);
		});
	})

	$(document).on('submit', '#show_program_date', function(){
		event.preventDefault();
		var cin_namme = $('#chosen_cinema').text();
		var date_indexx = '2';
		var current_date = $('#diff_chosen_date').val();
		var send = {cinemaname: cin_namme, index: date_indexx, program_date: current_date};
		ajaxrequest('programContent.php', 'POST', send, function(data){
			$('#program_content').remove();
			$('#maincontent').append('<div id="program_content"></div>');
			$('#program_content').append(data);
		});
	})
	
	$(document).on('click', '.hour_begin', function(){
		event.preventDefault();
		var proj_id = $(this).children('a').attr('id');
		var send = {projid: proj_id};
		ajaxrequest('reservationScheme.php', 'post', send, function(data){
			$('#maincontent').empty();
			$('#maincontent').append(data);
		});
	})
	
	//Reservation

	$(document).on('click', '.seat', function(){
		if ($(this).data('status')==0) {
			$(this).css('background-color', '#f3e11b');
			$(this).data('status', 1);		
		}
		else if ($(this).data('status')==1) {
			$(this).css('background-color', '#009c07');
			$(this).data('status', 0);
		};
	})

	$(document).on('click', '#reserve_butt', function(){
		event.preventDefault();
		var reservations = new Object();
		$('.seat').each(function(index){
			if ($(this).data('status')==1) {
				var key_res = 'seat'+index;
				reservations[key_res] = $(this).data('seat');
			};
		});
		reservations['proj_id'] = $('#reserve_butt').data('proj');
		console.log(reservations);
		ajaxrequest('reserve.php', 'post', reservations, function(data){
			homePage();
			messageBox(data, 4);
		});
	})

	//Home screen - cells
	$(document).on('click', '.movie-cell-more', function(){
		$('#maincontent').empty();
		event.preventDefault();
		var name = $(this).parent().children('.movie-cell-title').find('a').text();
		var send_data = { moviename: name};
		$('#maincontent').append('<div id="home-movie-content"></div>');
		ajaxrequest('movieDescription.php', 'POST', send_data, function(data){
			$('#home-movie-content').append(data);
		});
	})

	$(document).on('click', '.movie-cell-title', function(){
		$('#maincontent').empty();
		event.preventDefault();
		var name = $(this).find('a').text();
		var send_data = { moviename: name};
		$('#maincontent').append('<div id="home-movie-content"></div>');
		ajaxrequest('movieDescription.php', 'POST', send_data, function(data){
			$('#home-movie-content').append(data);
		});
	})

	$(document).on('click', '.movie-cell-cover', function(){
		$('#maincontent').empty();
		event.preventDefault();
		var name = $(this).parent().children('.movie-cell-title').find('a').text();
		var send_data = { moviename: name};
		$('#maincontent').append('<div id="home-movie-content"></div>');
		ajaxrequest('movieDescription.php', 'POST', send_data, function(data){		
			$('#home-movie-content').append(data);
		});
	})

	//User reservation statistic

	$('#stat-but').on('click', function(){
		$('#maincontent').empty();
		event.preventDefault();
		ajaxGetRequest('statistics.php', function(data){
			$('#maincontent').append(data);
		});
	})

	//Comments

	$(document).on('click', '.my-comment', function(){
		$('#comment-rate-bg').css('visibility','visible');
		var movie = $(this).data('movieid');
		var send_data = {movieid: movie};
		ajaxrequest('openWriteComment.php', 'POST', send_data, function(data){	
			 $(data).insertAfter('#content');
		});
	})

	$(document).on('click', '#cancel-comment', function(){
		$('#comment-rate-bg').css('visibility','hidden');
		$('#comment-box').remove();
	})

	$(document).on('click', '#send-comment', function(){
		$('#comment-rate-bg').css('visibility','hidden');
		$('#comment-box').remove();
		var movie = $(this).data('idmovie');
		var comment_content = $(this).parent().find('textarea').val();
		var send_data = {movieid: movie, commentcont: comment_content};
		ajaxrequest('writeComment.php', 'POST', send_data, function(data){	
			messageBox(data, 3);
		});
	})

	$(document).on('click', '#read-comment', function(){
		$('#comment-rate-bg').css('visibility','visible');
		var movie = $(this).data('movieid');
		var send_data = {movieid: movie};
		ajaxrequest('readComments.php', 'POST', send_data, function(data){	
			 $(data).insertAfter('#content');
			 console.log(send_data);
		});
	})

	$(document).on('click', '#close-comments-list', function(){
		$('#comment-rate-bg').css('visibility','hidden');
		$('#comments-list').remove();
		$('#close-comments-list').remove();
	})

	//Rating

	$(document).on('click', '.rate', function(){
		$('#comment-rate-bg').css('visibility','visible');
		$('<div id="rate-box"><span>Please rate the movie(1 is the lowest, 10 is the highest rate):</span><div id="radio-rate"><input type="radio" name="rate" value="1">1<input type="radio" name="rate" value="2">2<input type="radio" name="rate" value="3">3<input type="radio" name="rate" value="4">4<input type="radio" name="rate" value="5" checked>5<input type="radio" name="rate" value="6">6<input type="radio" name="rate" value="7">7<input type="radio" name="rate" value="8">8<input type="radio" name="rate" value="9">9<input type="radio" name="rate" value="10">10</div><div id="give-rate"><a href="#">Rate</a></div><div id="cancel-rate"><a href="#">Cancel</a></div></div>').insertAfter('#content');
	})

	$(document).on('click', '#cancel-rate', function(){
		$('#comment-rate-bg').css('visibility','hidden');
		$('#rate-box').remove();
	})

	$(document).on('click', '#give-rate', function(){
		var rating = $("input:radio[name=rate]:checked").val();
		var movieid = $('.my-comment').data('movieid');
		var send_data = {rate: rating, movie: movieid};
		ajaxrequest('openRate.php', 'POST', send_data, function(data){	
			$('#comment-rate-bg').css('visibility','hidden');
			$('#rate-box').remove();
			messageBox(data, 3);
		});
	})

	//Show projection in Movie description

	$(document).on('click', '#show-proj', function(){
		event.preventDefault();
		var movieid = $(this).data('movieid');
		var send_data = {movie: movieid};
		ajaxrequest('showOneMovieProgram.php', 'POST', send_data, function(data){
			$('#one-movie-program').remove();
			$('#movie_descr').append(data);
		});
	})

	$(document).on('submit', '#one-program', function(){
		event.preventDefault();
		ajaxrequest($('#one-program').attr('action'), $('#one-program').attr('method'), $('#one-program').serialize(), function(data){
			$('.one-program-hours').remove();
			$('#movie_descr').append(data);
		});
	})

})